//
//  EditTouristsListViewController.m
//  SXH
//
//  Created by Ryan on 16/3/24.
//  Copyright © 2016年 lyss. All rights reserved.
//

#import "EditTouristsListViewController.h"
#import "EditTouristCell.h"
#import "CommonService.h"
#import "TouristAddViewController.h"
@interface EditTouristsListViewController ()<UITableViewDelegate,UITableViewDataSource,EditTouristCellDelegate>
@property (nonatomic,strong) NSMutableArray *editTouristArray;
@end

@implementation EditTouristsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LIGHTGARY;
    [self getTouristInfo];
    self.totolNumLabel.text = [NSString stringWithFormat:@"%zi",self.childNum + self.adultNum + self.child_No_Bed_Num + self.olderNum + self.babyNum];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(@154);
        make.right.equalTo(@0);
        make.bottom.equalTo(@0);
    }];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.editTouristArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = @"EditTouristCell";
    EditTouristCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
        cell.delegate = self;
    }
    EditTouristModel *model = [self.editTouristArray objectAtIndex:indexPath.row];
    cell.model = model;
    cell.tag = indexPath.row;
    return cell;
}

- (UITableView*)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    }
    return _tableView;
}

- (void)getTouristInfo
{
    NSDictionary *parm = @{@"limit":@"50",
                           @"page":@"1",
                           @"insert_user_id":USERID};
    CommonService *commonService = [[CommonService alloc]init];
    [commonService getNetWorkData:parm Path:TRAVEL_TOURISTS Successed:^(NSDictionary* entity)
    {
        NSArray* array = [entity valueForKeyPath:@"rows"];
        NSError *error = nil;
        self.editTouristArray = [EditTouristModel arrayOfModelsFromDictionaries:array error:&error];
        for (int i = 0; i < [self.editTouristArray count]; i++) {
            for (int j = 0; j < [self.selectTouristArray count]; j++) {
                EditTouristModel *editmodel = [self.editTouristArray objectAtIndex:i];
                EditTouristModel *selectmodel = [self.selectTouristArray objectAtIndex:j];
                if ([editmodel.id isEqualToString:selectmodel.id])
                {
                    editmodel.isSelect = @"1";
                }
            }
        }
        [self.tableView reloadData];
    } Failed:^(NSInteger errorCode, NSString *message) {
        
    }];
}

- (void)gotoEditVC:(NSInteger)index
{
    TouristAddViewController *mvc = [[TouristAddViewController alloc]init];

    if (index >= 0)
    {
        mvc.model = [self.editTouristArray objectAtIndex:index];
    }
    mvc.parentVC = self;
    [self.navigationController pushViewController:mvc animated:YES];

}

- (IBAction)addNewTourist:(id)sender
{
    [self gotoEditVC:-1];
}

- (void)returnListModel:(ChangListModelBlock)block
{
    _changeListModelBlock = block;
}

- (IBAction)doBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];

}


- (IBAction)completeAndBack:(id)sender {
    NSInteger  selectAdultNum = 0;
    NSInteger  selectChildNum = 0;
    NSInteger  selectOlderNum = 0;
    NSInteger  selectChild_NoBed_Num = 0;
    NSInteger  selectBabyNum = 0;
    
    [self.selectTouristArray removeAllObjects];
    for (int i = 0 ; i < [self.editTouristArray count]; i++) {
        EditTouristModel *editmodel = [self.editTouristArray objectAtIndex:i];
        if ([editmodel.isSelect isEqualToString:@"1"]) {
            [self.selectTouristArray addObject:editmodel];
            switch ([editmodel.identity intValue]) {
                case 1:
                    selectAdultNum ++;
                    break;
                case 2:
                    selectOlderNum ++;
                    break;
                case 3:
                    selectChild_NoBed_Num ++;
                    break;
                case 4:
                    selectChildNum ++;
                    break;
                case 5:
                    selectBabyNum ++;
                    break;
                    
                default:
                    break;
            }
        }
    }
    if (selectAdultNum != self.adultNum||selectChildNum != self.childNum ||selectOlderNum !=self.olderNum  ||selectBabyNum != self.babyNum || selectChild_NoBed_Num != self.child_No_Bed_Num) {
        ALERT_ERR_MSG(@"请选择正确的游客信息");
        return;
    }
    
    [self.navigationController popViewControllerAnimated:YES];


    if (self.changeListModelBlock) {
        self.changeListModelBlock(self.selectTouristArray);
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
